import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter_base_library/log/log.dart';

class LogsInterceptor extends InterceptorsWrapper {
  static List<Map> sHttpResponses = [];
  static List<String> sResponsesHttpUrl = [];

  static List<Map<String, dynamic>> sHttpRequest = [];
  static List<String> sRequestHttpUrl = [];

  static List<Map<String, dynamic>> sHttpError = [];
  static List<String> sHttpErrorUrl = [];

  @override
  onRequest(RequestOptions options) async {
    Log.D("请求url：${options.path} ${options.method}");
    options.headers.forEach((k, v) => options.headers[k] = v ?? "");
    Log.D('请求头: ' + options.headers.toString());
    if (options.data != null) {
      Log.D('请求参数: ' + options.data.toString());
    }
    try {
      addLogic(sRequestHttpUrl, options.path);
      var data;
      if (options.data is Map) {
        data = options.data;
      } else {
        data = Map<String, dynamic>();
      }
      var map = {
        "header:": {...options.headers},
      };
      if (options.method == "POST") {
        map["data"] = data;
      }
      addLogic(sHttpRequest, map);
    } catch (e) {
      Log.Ex(e);
    }
    return options;
  }

  @override
  onResponse(Response response) async {
    try {
      Log.D('返回参数: ' + response.toString());
      if (response.data is Map || response.data is List) {
        try {
          var data = Map<String, dynamic>();
          data["data"] = response.data;
          addLogic(sResponsesHttpUrl, response.request.uri.toString());
          addLogic(sHttpResponses, data);
        } catch (e) {
          Log.Ex(e);
        }
      } else if (response.data is String) {
        try {
          var data = Map<String, dynamic>();
          data["data"] = response.data;
          addLogic(sResponsesHttpUrl, response.request.uri.toString());
          addLogic(sHttpResponses, data);
        } catch (e) {
          Log.Ex(e);
        }
      } else if (response.data != null) {
        try {
          String data = response.data.toJson();
          addLogic(sResponsesHttpUrl, response.request.uri.toString());
          addLogic(sHttpResponses, json.decode(data));
        } catch (e) {
          Log.Ex(e);
        }
      } // continue
    } catch (e) {
      Log.Ex('请求异常: ' + e.toString());
    }
    return response;
  }

  @override
  onError(DioError err) async {
    Log.Ex('请求异常: ' + err.toString());
    Log.Ex('请求异常信息: ' + (err.response?.toString() ?? ""));
    try {
      addLogic(sHttpErrorUrl, err.request.path);
      var errors = Map<String, dynamic>();
      errors["error"] = err.message;
      addLogic(sHttpError, errors);
    } catch (e) {
      Log.Ex(e);
    }
    return err;
  }

  static addLogic(List list, data) {
    if (list.length > 20) {
      list.removeAt(0);
    }
    list.add(data);
  }
}
